<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>图书详情</title>
    <link rel="stylesheet" type="text/css" href="/site/css/styles.css">
</head>
<body>
<div id="app">
    <app-header></app-header>
    <app-sidebar></app-sidebar>

    <div class="main-content">
        <h2>图书详情</h2>
        <div>
            <p><strong>书名:</strong> {{ book.title }}</p>
            <p><strong>作者:</strong> {{ book.author }}</p>
            <p><strong>ISBN:</strong> {{ book.isbn }}</p>
            <p><strong>出版社:</strong> {{ book.publisher }}</p>
            <p><strong>出版年份:</strong> {{ book.publicationYear }}</p>
            <p><strong>语言:</strong> {{ book.language }}</p>
            <p><strong>类别:</strong> {{ book.genre }}</p>
            <p><strong>馆藏位置:</strong> {{ book.location }}</p>
            <p><strong>图书状态:</strong>
                <span v-if="book.status === 1">在馆</span>
                <span v-else-if="book.status === 2">外借</span>
                <span v-else-if="book.status === 3">未上架</span>
                <span v-else>未知</span>
            </p>
            <p><strong>图书简介:</strong> {{ book.description }}</p>
            <a href="/book/list" class="back-button">返回图书列表</a>
        </div>
    </div>
</div>
<!-- 引入 Vue -->
<script src="/assets/vue/vue3.min.js"></script>
<!-- 引入 Axios -->
<script src="/assets/axios/axios.min.js"></script>
<!-- 引入组件 -->
<script src="/site/js/component/header.js"></script>
<script src="/site/js/component/sidebar.js"></script>

<script>
    const {reactive, createApp, onMounted} = Vue;

    const app = {
        // 使用局部注册的方式注册组件
        components: {
            AppHeader,
            AppSidebar
        },
        setup() {
            // 定义响应式数据 book
            const book = ref({});
            // 获取图书详情的方法
            const fetchBookById = () => {
                const urlParams = new URLSearchParams(window.location.search);
                const id = urlParams.get('id');
                if (id) {
                    axios.get(`/api/book/${id}`)
                        .then(response => {
                            if (response.data.code === 200) {
                                book.value = response.data.data;
                            } else {
                                console.error('获取图书详情失败:', response.data.message || '未知原因');
                                alert('获取图书详情失败，请稍后再试');
                            }
                        })
                        .catch(error => {
                            console.error('获取图书详情失败:', error);
                            alert('获取图书详情失败，请稍后再试');
                        });
                }
            };

            // 在组件挂载时调用 fetchBookById
            onMounted(() => {
                fetchBookById();
            });

            // 返回数据和方法给模板使用
            return {book};
        }
    };
    createApp(app).mount('#app');
</script>
</body>
</html>